package org.dtrd.modules.flup.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.flup.entity.po.DtrdRlFlupProjectRecord;
import org.dtrd.modules.flup.mapper.DtrdRlFlupProjectRecordMapper;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupTemplateProjectService;
import org.dtrd.modules.flup.service.IDtrdEntRdFlupTemplateService;
import org.dtrd.modules.flup.service.IDtrdRlFlupProjectRecordService;
import org.dtrd.modules.flup.service.IDtrdRlFlupTemplateProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 患者复诊项目检测数据 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-23
 */
@DS("multi-datasource1")
@Service
public class DtrdRlFlupProjectRecordServiceImpl extends ServiceImpl<DtrdRlFlupProjectRecordMapper, DtrdRlFlupProjectRecord> implements IDtrdRlFlupProjectRecordService {

    @Autowired
    private IDtrdEntRdFlupTemplateService templateService;
    @Autowired
    private IDtrdEntRdFlupTemplateProjectService projectService;
    @Autowired
    private IDtrdRlFlupTemplateProjectService templateProjectRlService;

    @Override
    public void createFlupFlow(Integer flupId, Integer templateId) {
        String templateName = templateService.getTemplateName(templateId);
        List<Integer> projectIds = templateProjectRlService.getProjectIds(templateId);

        List<DtrdRlFlupProjectRecord> templateProjects = new ArrayList<>();
        for (Integer projectId : projectIds) {
            DtrdRlFlupProjectRecord flupProjectRecord = new DtrdRlFlupProjectRecord();
            flupProjectRecord.setFlupAppointmentId(flupId);
            flupProjectRecord.setTemplateId(templateId);
            flupProjectRecord.setTemplateName(templateName);
            flupProjectRecord.setProjectId(projectId);
            String projectName = projectService.getProjectName(projectId);
            flupProjectRecord.setProjectName(projectName);
            templateProjects.add(flupProjectRecord);
        }
        saveBatch(templateProjects);
    }

    @Override
    public void removeFlupFlow(Integer flupId) {
        LambdaQueryWrapper<DtrdRlFlupProjectRecord> wrapper = Wrappers.lambdaQuery(DtrdRlFlupProjectRecord.class)
                .eq(DtrdRlFlupProjectRecord::getFlupAppointmentId, flupId);
        remove(wrapper);
    }

    public LambdaQueryWrapper<DtrdRlFlupProjectRecord> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdRlFlupProjectRecord.class)
                        .eq(DtrdRlFlupProjectRecord::getIsDel, 0);
    }

    public LambdaUpdateWrapper<DtrdRlFlupProjectRecord> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdRlFlupProjectRecord.class)
                        .eq(DtrdRlFlupProjectRecord::getIsDel, 0);
    }
}
